package com.china_goldcard.horizontallistview;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 水平菜单视图
 */
public class HorizontalMenuView extends RelativeLayout {

    public interface OnItemClickListener<T extends HorizontalMenuViewModel>{
        void onItemClick(T model,int position);
        void onItemLongClick(T model,int position);
    }


    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    private Context context;

    private RecyclerView recyclerView;

    private HorizontalMenuViewAdapter adapter;

    private int columnNum;

    private int menuBgColor;

    private float titleSize;

    private float itemH;

    private float itemW;

    private List<? extends HorizontalMenuViewModel> dataList = new ArrayList<>();

    public HorizontalMenuView(Context context){
        this(context,null);
    }

    public HorizontalMenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        getConfig(context,attrs);
        initView(context);
    }

    /**
     * 从xml中获取配置信息
     */
    private void getConfig(Context context, AttributeSet attrs) {
        //TypedArray是一个数组容器用于存放属性值
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.HorizontalMenuView);
        columnNum = attributes.getInteger(R.styleable.HorizontalMenuView_columnNum,4);
        titleSize = attributes.getDimensionPixelSize(R.styleable.HorizontalMenuView_menuTitleSize,(int)sp2px(12));
        itemH = attributes.getDimension(R.styleable.HorizontalMenuView_itemH,dp2px(30));
        menuBgColor = attributes.getColor(R.styleable.HorizontalMenuView_menuBgColor,getResources().getColor(R.color.menu_bg_color));
        //用完务必回收容器
        attributes.recycle();
        itemW = screenW(context) / columnNum;
    }

    /**
     * 初始化视图
     * @param context
     */
    private void initView(Context context){
        LayoutInflater.from(context).inflate(R.layout.horizontal_menu_view,this,true);
        recyclerView = (RecyclerView) findViewById(R.id.id_recyclerview_horizontal);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        updateUI();
    }

    /**
     * 更新UI
     */
    private void updateUI(){
        recyclerView.setBackgroundColor(menuBgColor);
    }


    /**
     * 加载数据
     * @param dataList
     * @param <M>
     */
    public <M extends HorizontalMenuViewModel> void loadData(List<M> dataList){
        loadData(dataList,null);
    }

    /**
     * 加载数据
     * @param dataList
     */
    public <T extends HorizontalMenuViewHolder,M extends HorizontalMenuViewModel> void loadData(List<M> dataList,HorizontalMenuAdapter<T,M> dataAdapter){
        this.dataList = dataList;
        adapter = new HorizontalMenuViewAdapter(dataList);
        if (dataAdapter != null){
            adapter.setDataAdapter(dataAdapter);
        }else{
            adapter.setDataAdapter(new DefaultHorizontalMenuAdapter(context,(int)itemW,(int)itemH,R.layout.horizontal_menu_view_item,titleSize));
        }
        recyclerView.setAdapter(adapter);
        adapter.setmOnItemClickLitener(new HorizontalMenuViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, HorizontalMenuViewModel item, int position) {
                if(onItemClickListener!=null){
                    onItemClickListener.onItemClick(item,position);
                }
            }
            @Override
            public void onItemLongClick(View view, HorizontalMenuViewModel item, int position) {
                if(onItemClickListener!=null){
                    onItemClickListener.onItemLongClick(item,position);
                }
            }
        });
    }

    public float dp2px(float dp) {
        final float scale = getResources().getDisplayMetrics().density;
        return dp * scale + 0.5f;
    }

    public float sp2px(float sp) {
        final float scale = getResources().getDisplayMetrics().scaledDensity;
        return sp * scale;
    }

    /**
     * 获得屏幕宽度
     * @param context
     * @return
     */
    public int screenW(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }
}
